<!DOCTYPE html>
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="description" content="百度地图API自定义地图，帮助用户在可视化操作下生成百度地图" />
   
   <title>位置</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
    <link rel="stylesheet" href="http://demo.open.weixin.qq.com/jssdk/css/style.css">
    <!--引用百度地图API-->
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=wzYBbMD1vRChljfWNuFKXx4UxP5cRmvD"></script>
    <style type="text/css">
	body, html,#map {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
	
	#r-result,#r-result table{width:100%;}
	</style>
</head>
<body ontouchstart="">
 <!--百度地图容器-->
    <div id="map">
    </div>
    <div id="r-result"></div>
    
</body>
<script src="__assets__/js/jquery.min.js"></script>


<script type="text/javascript">
var str={$res};
//console.log(str);
//在异步的问题的时候如果使用的是 controller model view 自动的进行json字符串进行封装 如果涉及到需要的字符串以字符串json格式进行封装
var o_lng=str.o_lng;
var o_lat=str.o_lat;
initMap(o_lng,o_lat);
 function initMap(longitude, latitude){
     createMap(longitude, latitude);//创建地图
     setMapEvent();//设置地图事件
     addMapControl();//向地图添加控件
     //addMapOverlay();//向地图添加覆盖物
     
   }
 function createMap(longitude, latitude){ 
     map = new BMap.Map("map"); 
     var point = new BMap.Point(longitude, latitude);
     map.centerAndZoom(point, 15);
    // map.addEventListener("tilesloaded",function(){alert("地图加载完毕");});
     var marker = new BMap.Marker(point); 
      // 创建标注
     
    // marker.addEventListener("click",attribute);
     
 	 map.addOverlay(marker);  
 	   // 将标注添加到地图中
 	 marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
  	var local = new BMap.LocalSearch(map, {
		  renderOptions:{map: map}
		});
  	local.searchInBounds(str.d_address, map.getBounds());
  	map.addEventListener("dragend",function(){
  	    map.clearOverlays();
  	    local.searchInBounds(str.d_address,map.getBounds());
  	});

  
  	
  	map.addEventListener('click',showInfo);


//   	var p1 = new BMap.Point(longitude,latitude);
// 	var p2 = new BMap.Point(data['0'],data['1']);
//   	var transit = new BMap.TransitRoute(map, {
//  		 renderOptions:{map: map}

//   	});
//   	transit.search(p1,p2);
   }
   function setMapEvent(){
     map.enableScrollWheelZoom();
     map.enableKeyboard();
     map.enableDragging();
     map.enableDoubleClickZoom()
   }
   function addClickHandler(target,window){
     target.addEventListener("click",function(){
       target.openInfoWindow(window);
     });
   }
   function addMapOverlay(){
   }
   //向地图添加控件
   function addMapControl(){
     var scaleControl = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT});
     scaleControl.setUnit(BMAP_UNIT_IMPERIAL);
     map.addControl(scaleControl);
     var navControl = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE});
     map.addControl(navControl);
     var overviewControl = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:true});
     map.addControl(overviewControl);
   }
//地图点击事件
   function showInfo(e)
   {
// 	   data.push(e.point.lng);
// 	   data.push(e.point.lat);
	  $('#map').hide();
	   var p1 = new BMap.Point(o_lng,o_lat);
	   var p2 = new BMap.Point(e.point.lng,e.point.lat);

	   var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map,autoViewport:true,panel:"r-result"}});
	   driving.search(p1,p2);
	  // alert(e.point.lng + ", " + e.point.lat);
   } 
   //注册覆盖图事件
   function attribute()
   {
	   var p=marker.getPostion();//获取marker的位置
	   alert('marker的位置是:'+p.lng+","+p.lat);
   }   
     
 
</script>
</html>